package com.prestamomo.vil.ppa.pmvilmedium;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;

import com.prestamomo.vil.ppa.PmvilApp;
import com.prestamomo.vil.ppa.pmvilapi.PmvilMemo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class PmvilMemoMd {
    @SuppressLint("Range")
    public static PmvilMemo getUserPmvilMemo(){
        PmvilMemo pmvilMemo = new PmvilMemo();
        List<PmvilMemo.DataListPmvil> pmvilMemoList = new ArrayList<>();

        String startTime = "";
        String endTime = "";
        Cursor eventCursor= null;
        try {
            eventCursor = PmvilApp.getPmvilApss().getContentResolver().query(Uri.parse("content://com.android.calendar/events"), null,
                    null, null, "dtstart" + " DESC");
            while (eventCursor != null && eventCursor.moveToNext()) {
                startTime = eventCursor.getString(eventCursor.getColumnIndex("dtstart"));
                endTime = eventCursor.getString(eventCursor.getColumnIndex("dtend"));
                PmvilMemo.DataListPmvil dataListPmvil = new PmvilMemo.DataListPmvil();

                dataListPmvil.setXpejosr(TextUtils.isEmpty(eventCursor.getString(eventCursor.getColumnIndex("title")))
                        ?"":eventCursor.getString(eventCursor.getColumnIndex("title")));//eventTitle
                dataListPmvil.setQlfiodwb(TextUtils.isEmpty(eventCursor.getString(eventCursor.getColumnIndex("description")))
                ?"":eventCursor.getString(eventCursor.getColumnIndex("description")));//description
                if (TextUtils.isEmpty(startTime) && TextUtils.isEmpty(endTime)) {
                    dataListPmvil.setXgaqtkac("");//startTime
                    dataListPmvil.setMwxulq("");//endTime
                } else {
                    if (!TextUtils.isEmpty(startTime) && !TextUtils.isEmpty(endTime)) {
                        startTime = getStrPmvilForLog(Long.parseLong(startTime), "yyyy-MM-dd HH:mm:ss.SSS").replace(" 24", " 00");
                        endTime = getStrPmvilForLog(Long.parseLong(endTime), "yyyy-MM-dd HH:mm:ss.SSS").replace(" 24", " 00");
                    }else {
                        if (TextUtils.isEmpty(startTime) && !TextUtils.isEmpty(endTime)) {
                            startTime = getStrPmvilForLog(Long.parseLong(startTime), "yyyy-MM-dd HH:mm:ss.SSS").replace(" 24", " 00");
                            endTime = startTime;
                        } else if (!TextUtils.isEmpty(startTime) && TextUtils.isEmpty(endTime)) {
                            endTime = getStrPmvilForLog(Long.parseLong(endTime), "yyyy-MM-dd HH:mm:ss.SSS").replace(" 24", " 00");
                            startTime = endTime;
                        }
                    }
                    dataListPmvil.setXgaqtkac(startTime);//startTime
                    dataListPmvil.setMwxulq(endTime);//endTime
                }
                pmvilMemoList.add(dataListPmvil);
            }
        } catch (Exception e) {

        } finally {
            if(eventCursor!=null){
                eventCursor.close();
            }
        }
        pmvilMemo.setOxishefxu("calendar");//type
        pmvilMemo.setDkhnvwe(pmvilMemoList);//dataList
        return pmvilMemo;
    }
    public static String getStrPmvilForLog(long pmviltimeStamp, String pmvilFormat) {
        return new SimpleDateFormat(pmvilFormat).format(pmviltimeStamp);

    }
}
